<?php

namespace Common\Model;

/**
 * Description of RoomInfoModel
 * @author songanwei
 * @date 2015-12-31 
 * @index roominfo(`isdelete`, `hotelentity_id`)
 */
class RoomInfoModel extends BaseModel {

    protected $trueTableName = 'tb_room_info';

    /**
     * 返回房间数据
     * @modify songanwei 2015-12-31
     * @modify trainyao 2016-01-04 加上索引查询
     * @modify trainyao 2016-01-05 加上多个id查询
     * @param integer $roomId 房间ID
     * @return type
     */
    public function getInfo($roomId) {
        if (is_array($roomId)) {
            $roomId = implode(',', $roomId);
        }

        $option = array(
            'isdelete' => NOT_DELETE,
        );

        if (strpos($roomId, ',') !== false) {
            $option['id'] = array('in', $roomId);
            return $this->where($option)->select();
        } else {
            $option['id'] = $roomId;
            return $this->where($option)->find();
        }
    }

    /**
     * 根据客栈获取房间
     * @modify trainyao 2016-01-07 创建
     * @modify songanwei 2016-01-28 修改
     * @modify trainyao 2016-04-25 修改为可获取已删除的
     * @param  integer   $hotelId   客栈id
     * @param  $roomFrom  房间来源
     * @param  mixin   $getDeleted  是否获取已删除的
     * @return array              房间数组
     */
    public function getInfoByHotel($hotelId, $roomFrom = null, $getDeleted = false) {
        if ($getDeleted !== false) {
            $option = array(
                'isdelete' => array('in', array(NOT_DELETE, IS_DELETE)),
                'hotelentity_id' => $hotelId,
            );
        } else {
            $option = array(
                'isdelete' => NOT_DELETE,
                'hotelentity_id' => $hotelId,
            );
        }
        if (!is_null($roomFrom)) {
            if(is_array($roomFrom)){
                $option['roomfrom'] = array('in',$roomFrom);
            }else{
                $option['roomfrom'] = $roomFrom; 
            }
        }
        $field = 'id,isdelete,hotelentity_id,roomtype_id,name,roomfrom,floornumber,layout,isdirty,starttime,endtime';
        $data = $this->where($option)->field($field)->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 客栈房间号返回数据
     * @modify songanwei 2015-12-31
     * @modify trainyao 2016-01-04 加上索引查询
	 * @modify zhangzhen 2016-12-27 加上房间来源
     * @param integer $hotelId  客栈ID
     * @param string $name 房间号
     */
    public function getInfoByName($hotelId, $name ,$roomFrom) {
        $where = array(
            'isdelete' => NOT_DELETE,
            'hotelentity_id' => $hotelId,
            'name' => $name
        );
		if (!is_null($roomFrom)) {
            if(is_array($roomFrom)){
                $option['roomfrom'] = array('in',$roomFrom);
            }else{
                $option['roomfrom'] = $roomFrom; 
            }
        }
        return $this->where($where)->find();
    }

    /**
     * 根据房型ID获取
     * @modify trainyao 2016-01-04  创建
     * @modify trainyao 2016-04-25 修改为可获取已删除的
     * @param  integer   $hotelId    客栈id
     * @param  mixin   $roomTypeId 房型id(s)
     * @param  mixin   $getDeleted  是否获取已删除的
     * @return array 房型房间数组
     */
    public function getInfoByRoomType($hotelId, $roomTypeId, $getDeleted = false) {
        if ($getDeleted !== false) {
            $option = array(
                'isdelete' => array('in', array(NOT_DELETE, IS_DELETE)),
                'hotelentity_id' => $hotelId,
            );
        } else {
            $option = array(
                'isdelete' => NOT_DELETE,
                'hotelentity_id' => $hotelId,
            );
        }

        if (is_array($hotelId)) {
            $option['hotelentity_id'] = array('in', $hotelId);
        }

        $field = 'id,isdelete,hotelentity_id,roomtype_id,name,roomfrom,floornumber,layout,isdirty';

        if (is_array($roomTypeId)) {
            $roomTypeId = implode(',', $roomTypeId);
        }

        if (strpos($roomTypeId, ',') !== false) {
            $option['roomtype_id'] = array('in', $roomTypeId);
        } else {
            $option['roomtype_id'] = $roomTypeId;
        }

        $data = $this->where($option)->field($field)->select();

        return empty($data) ? array() : $data;
    }
	
	/**
     * 客栈房间号返回数据
     * @modify zhangzhen 2016-12-27
     * @modify zhangzhen 2017-01-10 支持RF卡
     * @param integer $hotelId  客栈ID
     * @param string $name 房间号
	 * @param  mixin   $roomTypeId 房型id
	 * @param  $roomFrom  房间来源
     */
    public function getInfoByRoomTypeAndRoomFrom($hotelId, $roomTypeId, $roomFrom) {
        $where = array(
            'isdelete' => NOT_DELETE,
            'hotelentity_id' => $hotelId,
            'roomtype_id' => $roomTypeId,
			'roomfrom' => array('in',$roomFrom)
		);
        return $this->field('id,name')->where($where)->select();
    }
	
    /**
     * 添加房间数据
     * @Modify songanwei 2015-12-31
     * @Modify trainyao 2016-01-04 支持添加一个多个
     * @param integer $hotelId 客栈ID
     * @param integer $roomTypeId  房间类型ID
     * @param array $roomData 房间信息数组
     * @return type
     */
    public function addInfo($hotelId, $roomTypeId, $roomData) {
        $data = array();
        $result = null;

        if (count($roomData) == 1) {
            $data = array(
                'hotelentity_id' => $hotelId,
                'roomtype_id' => $roomTypeId,
                'name' => $roomData[0]['name'],
                'roomfrom' => $roomData[0]['roomfrom'],
            );
            $result = $this->add($data);
        } else {
            $userId = getUserId();
            foreach ($roomData as $roomDataItem) {
                array_push($data,
                        array(
                    'hotelentity_id' => $hotelId,
                    'roomtype_id' => $roomTypeId,
                    'name' => $roomDataItem['name'],
                    'roomfrom' => $roomDataItem['roomfrom'],
                    'creator' => $userId,
                    'operator' => $userId,
                ));
            }
            $result = $this->addAll($data);
        }
        return $result === false ? false : true;
    }
	/**
     * 同步锁掌柜房间数据
     * @Modify zhangzhen 2016-12-27 支持添加一个多个
     * @modify zhangzhen 2017-01-10 支持RF卡
     * @param integer $hotelId 客栈ID
     * @param integer $roomTypeId  房间类型ID
     * @param array $roomData 房间信息数组
     * @return type
     */
    public function addSynInfo($hotelId, $roomTypeId, $roomData) {
		$data = array(
			'hotelentity_id' => $hotelId,
			'roomtype_id' => $roomTypeId,
			'name' => $roomData['name'],
			'roomfrom' => $roomData['roomfrom'],
		);
        return $this->add($data);
    }
    /**
     * 设置脏房
     * @modify songanwei 2015-12-31
     * @modify traiynao  2016-01-07 增加同时操作多个房间脏净值
     * @param integer $hotelId  客栈ID
     * @param integer $roomId  房间ID
     * @param integer $dirty 0=非脏房 1=脏房
     * @return type
     */
    public function setDirty($hotelId, $roomId, $dirty = 1) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'hotelentity_id' => $hotelId,
        );
        if (is_array($roomId)) {
            $roomId = implode(',', $roomId);
        }
        if (strpos($roomId, ',') !== false) {
            $option['id'] = array('in', $roomId);
        } else {
            $option['id'] = $roomId;
        }

        $result = $this->where($option)->save(array('isdirty' => $dirty));
        return $result === false ? false : true;
    }

    /**
     * 更新房间数据
     * @modify trainyao 2016-01-04   创建
     * @param  integer   $hotelId     客栈id
     * @param  integer   $roomId     房间id
     * @param  string   $roomName    房间号
     * @param  integer  $roomFrom    房间来源
     * @param  string   $floorNumber 房间层数
     * @param  string   $layout      房间布局
     * @return boolean 执行结果
     */
    public function editInfo($hotelId, $roomId, $roomName = NULL, $roomFrom = NULL, $floorNumber = NULL, $layout = NULL) {
        $query = array();
        $option = array(
            'isdelete' => NOT_DELETE,
            'hotelentity_id' => $hotelId,
            'id' => $roomId,
        );

        $data = array();
        if (!is_null($roomName)) {
            $data['name'] = $roomName;
        }
        if (!is_null($roomFrom)) {
            $data['roomfrom'] = $roomFrom;
        }
        if (!is_null($floorNumber)) {
            $data['floornumber'] = $floorNumber;
        }
        if (!is_null($layout)) {
            $data['layout'] = $layout;
        }

        return $this->where($option)->save($data) === false ? false : true;
    }

    /**
     * 根据ID删除数据
     * @modify Amos 2015-09-25 创建
     * @modify trainyao 2016-01-04 增加根据多个id删除
     * @param mixin $ids
     * @return boolean 删除成功与否
     */
    public function deleteInfo($id) {
        $option = array(
            'isdelete' => NOT_DELETE,
        );

        if (is_array($id)) {
            $id = implode(',', $id);
        }
        if (strpos($id, ',') !== false) {
            $option['id'] = array('in', $id);
        } else {
            $option['id'] = $id;
        }

        $data = array(
            'isdelete' => IS_DELETE,
            'endtime' => date('Y-m-d H:i:s')
        );

        $result = $this->where($option)->save($data);
        return $result === false ? false : true;
    }

    /**
     * 返回房间数据 需要返回二维数组
     * @modify wenhui 2016-12-01
     * @param array $roomId 房间ID
     * @return array 返回二维数组
     */
    public function getRoomInfo($roomId) {
        if (is_array($roomId)) {
            $roomId = implode(',', $roomId);
        }

        $option = array(
            'isdelete' => NOT_DELETE,
        );

        $option['id'] = array('in', $roomId);
        return $this->where($option)->select();
    }

    /**
     * 返回房间数据 需要返回二维数组
     * @modify wenhui 2016-12-01
     * @param array $roomTypeId 房间ID
     * @return array 返回二维数组
     */
    public function getRoomByTypeId($roomTypeId) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'roomtype_id' => $roomTypeId,
        );

        return $this->field('id,name')->where($option)->select();
    }
}
